package com.xiaoyu.linkedArray;

/**
 * @program: DS_and_A
 * @description:  链表的中间结点!
 *
 * 给定一个头结点为 head 的非空单链表，返回链表的中间结点。
 *
 * 如果有两个中间结点，则返回第二个中间结点。
 *
 * @author: YuWenYi
 * @create: 2021-06-02 10:16
 **/
public class MiddleNode_876 {

    public static ListNode middleNode(ListNode head) {
        ListNode fast = head,slow = head;
        while (fast!=null && fast.next!=null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1,new ListNode(2,
                new ListNode(3,new ListNode(4,new ListNode(8,
                        new ListNode(7,new ListNode(6,new ListNode(5))))))));

        ListNode node = middleNode(head);
        ListNode.printList(node);
    }
}
